<?php
/**
 * Partial Loop for Numerically Indexed Arrays
 * Because the standard PartialLoop helper requires that you have an array of
 * associative arrays, it is not very useful for looping over say, rowsets. This
 * partial loop helper accepts a single-dimensional array and assigns each
 * element to a variable called "element" which can be used in the template.
 * Other than that, it works exactly as Zend_View_Helper_PartialLoop does.
 *
 * @package     Visi
 * @subpackage  View Helpers
 * @author      Luke Visinoni <luke.visinoni@gmail.com>
 * @copyright   (c) 2011 Coalition Technologies <joel@coalitiontechnologies.com>
 * @version     $Id$
 * @todo        I finally thought of a good name for this... PartialRepeat()
 */
class Visi_View_Helper_PartialIterate extends Zend_View_Helper_Partial {

    /**
     * Marker to where the pointer is at in the loop
     * @var integer
     */
    protected $partialCounter = 0;
    
    /**
     * Loop over a single-dimensional array (or set) and render a partial for
     * each item in it.
     *
     * If no arguments are provided, returns object instance.
     *
     * @param  string $name         Name of view script
     * @param  string|array $module If $model is empty, and $module is an array,
     *                              these are the variables to populate in the
     *                              view. Otherwise, the module in which the
     *                              partial resides
     * @param  array $model         Variables to populate in the view
     * @param  string $elementName  The name that should be used inside the
     *                              partial template for each element.
     * @param  array $extraContext  Used to provide extra variables for use
     *                              within the partial loop.
     * @return string|$this         Returns either the rendered loop or, if
     *                              there were no parameters passed in, it will
     *                              return $this.
     * @todo Take a look at the partialLoop() helper to see why $this would need
     *       to be returned when no variables are passed in.
     */
    public function partialIterate($name = null, $module = null, $model = null, $elementName = 'element', $extraContext = array()) {
    
        if (0 == func_num_args()) {
            return $this;
        }
        
        if ((null === $model) && (null !== $module)) {
            $model  = $module;
            $module = null;
        }
        
        if (!is_array($model)
            && (!$model instanceof Traversable)
            && (is_object($model) && !method_exists($model, 'toArray'))
        ) {
            $e = new Zend_View_Helper_Partial_Exception('PartialIterate helper requires iterable data');
            $e->setView($this->view);
            throw $e;
        }
        
        if (is_object($model)
            && (!$model instanceof Traversable)
            && method_exists($model, 'toArray')
        ) {
            $model = $model->toArray();
        }
        
        $content = '';
        $this->partialCounter = 0;
        foreach ($model as $key => $item) {
            // increment the counter variable
            $this->partialCounter++;
            $item = array(
                'key' => $key,
                $elementName => $item,
            );
            $item = array_merge($extraContext, $item); 
            $content .= $this->partial($name, $module, $item);
        }
        
        return $content;
    
    }
}